﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using net.xBei.WebApi.Helper;

namespace net.xBei.WebApi.FrameworkExtensions {
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
    public static class CustomSessionMiddlewareExtensions {
        public static IApplicationBuilder UseCustomSession(
            this IApplicationBuilder builder) {
            return builder.UseMiddleware<CustomSessionMiddleware>();
        }
    }
    class CustomSessionMiddleware {
        private readonly RequestDelegate _next;

        public CustomSessionMiddleware(RequestDelegate next) {
            _next = next;
        }

        public Task Invoke(HttpContext context) {
            var userId = context.User.Identity?.Name;
            if (!string.IsNullOrEmpty(userId) && !context.IsSystemUser()) {
                //TODO:
                //CommonFuncs.TouchUserSession(userId, context.User.FindFirst(claim => claim.Type == System.Security.Claims.ClaimTypes.Sid).Value);
            }
            // Call the next delegate/middleware in the pipeline
            return _next(context);
        }
    }
}
